import {query, add, remove, edit, editStatus, resetPassword, editApply} from '@/services/user_manage';
import  moment from 'moment';

export default {
  namespace: 'user_manage',
  state: {
    pageLoaded: false,
    data: [],
    selectedRowKeys: [],
    scrollData: {x: 0, y: 0},
    searchParams: {
      username: '',
      started_at: moment().format('YYYY-MM-DD 00:00:00'),
      ended_at: moment().format('YYYY-MM-DD HH:mm:ss'),
      page: 1,
      per_page: 30,
    },
    meta: {pagination: {total: 0, per_page: 0, current_page: 1}},
    loading: true,

  },
  effects: {
    *reset({payload, callback}, {call, put}) {
      console.log('reset')
      yield put({
        type: 'resetData',
        payload: payload
      });
      if (callback) callback()
    },
    *changeLoaded({payload, callback}, {call, put}) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({payload, callback}, {call, put}) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.user_manage)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *fetch({payload, callback}, {call, put, select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });

      const state = yield select(state => state.user_manage)//获取state中的数据
      console.log('state', state)
      payload = payload || state.searchParams;
      console.log('payload', payload)
      const response = yield call(query, payload);
      console.log(response)
      if (response.status === 200) {
        yield put({
          type: 'save',
          payload: response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }

    },
    *add({payload, callback}, {call, put}) {
      const response = yield call(add, payload);

      console.log(response)
      if (response.status === 200) {
        if (callback) callback();
      } else {
      }
    },
    *edit({payload, callback}, {call, put}) {
      console.log('edit')
      const response = yield call(edit, payload);
      console.log(response)
      if (response.status === 200) {
        if (callback) callback();
      }
    },
    *editApply({payload, callback}, {call, put}) {
      console.log('edit')
      const response = yield call(editApply, payload);
      console.log(response)
      if (response.status === 200) {
        if (callback) callback();
      }
    },
    *editStatus({payload, callback}, {call, put}) {
      console.log('payload', payload)
      const response = yield call(editStatus, payload);
      console.log(response)
      if (response.status === 200) {
        if (callback) callback();
      }
    },
    *remove({payload, callback}, {call, put}) {
      const response = yield call(remove, payload);
      if (response.status === 200) {
        if (callback) callback();
      }
    },
    *resetPassword({payload, callback}, {call, put}) {
      const response = yield call(resetPassword, payload);
      if (response.status === 200) {
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: [...action.payload.data],
        meta: action.payload.meta
      };
    },
    saveAndPush(state, action) {
      // console.log('[...state.data,...action.payload.data]',[...state.data,...action.payload.data]);
      const data = [...state.data, ...action.payload.data];
      // data.map((item,index)=>{
      //   item.index=index
      // })
      return {
        ...state,
        data: data,
        meta: action.payload.meta
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: action.payload,
      };
    },
    resetData(state, action) {
      return {
        pageLoaded: action.payload,
        data: [],
        selectedRowKeys: [],
        scrollData: {x: 0, y: 0},
        searchParams: {
          username: '',
          page: 1,
          started_at: moment().format('YYYY-MM-DD 00:00:00'),
          ended_at: moment().format('YYYY-MM-DD HH:mm:ss'),
          per_page: 30,
        },
        meta: {pagination: {total: 0, per_page: 0, current_page: 1}},
        loading: true,

      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
  },
};
